package pro.lepers.portfolio.view.contents.portfolio.thumb {
	import flash.display.LineScaleMode;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;

	/**
	 * @author plepers
	 */
	public class Holder extends Sprite {
		
		
		protected static const BG_COLOR : uint = 0xfcfaf6;
		
		protected static const BORDER_SIZE : int = 8;
				
		protected static const BORDER_COLOR : uint = 0x3A2C1D;
		
		
		public function Holder ( w : int, h : int ) {
			_width = w;
			_height = h;
			_build( );
			
			addEventListener( Event.ADDED_TO_STAGE, onAdded );
			addEventListener( Event.REMOVED_FROM_STAGE, onRemoved );
		}

		private function onAdded( e : Event ) : void {
			if( !_sizeValid ) {
				stage.addEventListener( Event.RENDER, _setSize );
				stage.invalidate();
			}
		}
		
		private function onRemoved( e : Event ) : void {
			stage.removeEventListener( Event.RENDER, _setSize );
		}
		
		
		public function get altitude () : Number {
			return _alt;
		}
		
		public function set altitude (alt : Number) : void {
			if( _alt == alt ) return;
 			_alt = alt;
			_shadow.distance = alt * 8;
			_shadow.alpha = alt * 0.1 + 0.24;
			_bgShape.filters = [ _shadow ];
		}

		protected function _build () : void {
			_bgShape = new Shape();
			
			_bgShape.graphics.lineStyle( 1, BORDER_COLOR , .1, true, LineScaleMode.NONE );
			_bgShape.graphics.beginFill( BG_COLOR );
			_bgShape.graphics.drawRect( 0 , 0 , 200 , 200 );
			
			_bgShape.width = _width+ BORDER_SIZE*2;
			_bgShape.height = _height+ BORDER_SIZE*2;
			
			_bgShape.x = -( _width >> 1 )- BORDER_SIZE;
			_bgShape.y = -( _height >> 1 ) - BORDER_SIZE;
			
			_bgShape.filters = [ _shadow = new DropShadowFilter( 6, 45, 0, .34, 16, 16, 1, 3 ) ];
			
			addChild( _bgShape );
			
			addChild( _container = new Sprite() );
			_container.x = -( _width >> 1 );
			_container.y = -( _height >> 1 );
		}

		
		
		override public function set width (value : Number) : void {
			_width = value;
			_invalidateSize();
		}

		override public function set height (value : Number) : void {
			_height = value;
			_invalidateSize();
		}
		
		
		
		override public function get width () : Number {
			return _width;
		}

		override public function get height () : Number {
			return _height;
		}
		
		
		public function get nativeHeight() : Number {
			return _nativeHeight;
		}
		
		public function get nativeWidth() : Number {
			return _nativeWidth;
		}
		
		protected function setSize( w : int, h : int ) : void {
			_bgShape.width = w + BORDER_SIZE*2;
			_bgShape.height = h + BORDER_SIZE*2;
			
			_bgShape.x = -( w >> 1 )- BORDER_SIZE;
			_bgShape.y = -( h >> 1 ) - BORDER_SIZE;
			
			_container.x = -( w >> 1 );
			_container.y = -( h >> 1 );
		}
		
		
		private function _invalidateSize() : void {
			
			if( ! _sizeValid ) return;
			if( stage ) {
				stage.addEventListener( Event.RENDER, _setSize );
				stage.invalidate();
			} else 
				addEventListener( Event.ADDED_TO_STAGE , _setSize );
			
			_sizeValid = false;
		}
		
		private function _setSize(event : Event) : void {
			_sizeValid = true;
			
			setSize( _width , _height );
			
			if( stage )
				stage.removeEventListener( Event.RENDER, _setSize );
			removeEventListener( Event.ADDED_TO_STAGE , _setSize );
		}

		
		protected var _container : Sprite;
		
		protected var _height : Number;
		protected var _width : Number;

		protected var _nativeHeight : Number;
		protected var _nativeWidth : Number;
		
		protected var _locked : Boolean = false;

		private var _bgShape : Shape;

		private var _shadow : DropShadowFilter; 

		private var _sizeValid : Boolean = true;
		
		private var _alt : Number = 1;

		
		
		public function get locked () : Boolean {
			return _locked;
		}
		
		public function set locked (locked : Boolean) : void {
			_locked = locked;
			cacheAsBitmap = locked;
			_shadow.quality = locked ? 3 : 1;
			_shadow.strength = locked ? 1 : .8;
			_bgShape.filters = [ _shadow ];
		}
		
	}
}
